package com.itheima.service.demo;

import com.itheima.bean.Student;
import com.itheima.dao.StudentDao;
import com.itheima.service.StudentService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentServiceDemo implements StudentService {
    @Override
    public List<Student> selectAll() {
        InputStream is = null;
        SqlSession sqlSession = null;
        List<Student> list = null;
        try {
            // 加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 通过SqlSession工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 获取 studentDao接口的实现类对象
            StudentDao mapper = sqlSession.getMapper(StudentDao.class);
            // 调用实现类对象中的方法完成操作
            list = mapper.selectAll();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        return list;
    }

    // 根据id查询
    public Student selectById(Integer id){
        InputStream is = null;
        SqlSession sqlSession = null;
        Student stu = null;
        try {
            // 加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            //
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //
            sqlSession = sqlSessionFactory.openSession(true);
            //
            StudentDao mapper = sqlSession.getMapper(StudentDao.class);
            //
            stu = mapper.selectById(id);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        return stu;
    }

    // 新增
    @Override
    public Integer insert(Student stu) {
        InputStream is = null;
        SqlSession sqlSession = null;
        int i = 0;
        try {
            // 加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 通过SqlSession工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 获取 studentDao接口的实现类对象
            StudentDao mapper = sqlSession.getMapper(StudentDao.class);
            // 调用实现类对象中的方法完成操作
            i = mapper.insert(stu);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        return i;
    }

    // 修改
    @Override
    public Integer update(Student stu) {
        InputStream is = null;
        SqlSession sqlSession = null;
        int i = 0;
        try {
            // 加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 通过SqlSession工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 获取 studentDao接口的实现类对象
            StudentDao mapper = sqlSession.getMapper(StudentDao.class);
            // 调用实现类对象中的方法完成操作
            i = mapper.update(stu);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        return i;
    }

    // 删除
    @Override
    public Integer delete(Integer id) {
        InputStream is = null;
        SqlSession sqlSession = null;
        int i = 0;
        try {
            // 加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 通过SqlSession工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 获取 studentDao接口的实现类对象
            StudentDao mapper = sqlSession.getMapper(StudentDao.class);
            // 调用实现类对象中的方法完成操作
            i = mapper.delete(id);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        return i;
    }


    @Override
    public List<Student> selectByNameOrAge(String name,Integer age) {
        InputStream is = null;
        SqlSession sqlSession = null;
        List<Student> list = null;
        try {
            // 加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            //
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //
            sqlSession = sqlSessionFactory.openSession(true);
            //
            StudentDao mapper = sqlSession.getMapper(StudentDao.class);
            //
            list = mapper.selectByNameOrAge(name, age);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        return list;
    }
}
